package com.cowin.inf.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.cowin.base.result.Result;
import com.cowin.base.result.ResultGenerator;
import com.cowin.inf.model.KyLineState;
import com.cowin.inf.service.KyLineStateService;
import com.cowin.sys.model.TmSysParameter;
import com.cowin.sys.model.vo.EmosCarInfo;
import com.cowin.sys.service.TmOfmOrderService;
import com.cowin.sys.service.TmSysParameterService;
import com.cowin.utils.JDBCUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C), 1998-2019, 奇瑞信息
 *
 * @author : 于超
 * @Date: 2019/12/26 16:10
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("inf/emos")
@Api(value = "EmosController", description = "涂装车间控制器", tags = "EmosController", consumes = "application/json")
public class EmosController {
    @Resource
    private TmSysParameterService tmSysParameterService;

    @Resource
    private TmOfmOrderService tmOfmOrderService;

    @Resource
    private KyLineStateService kyLineStateService;

    @Value("${spring.profiles.active}")
    private String env;

    @GetMapping("/line/state/job")
    @ApiOperation(value = "涂装产线查询", notes = "涂装产线查询")
    public Result emosLineStateJob() {
//        log.info("EMOS 产线状态查询...........");
        TmSysParameter emosDbConfig = tmSysParameterService.getEmosDbConfig();
        Connection conn = JDBCUtils.getConnection(emosDbConfig.getParameterValue().trim(), emosDbConfig.getParameterCategory().trim(), emosDbConfig.getNotes1().trim());
        String sql = "select * from tblLineStatus";
        List<KyLineState> list = new ArrayList<>();
        Statement statement = null;
        ResultSet rs = null;
        try {
            statement = conn.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                KyLineState kyLineState = new KyLineState();
                kyLineState.setStatus(rs.getString("status"));
                kyLineState.setNodeTime(DateUtil.parseDate(rs.getString("nodeTime")));
                kyLineState.setBreakReason(rs.getString("breakReason"));
                kyLineState.setLineNo(rs.getString("lineNo"));
                kyLineState.setPlantNo(rs.getString("plantNo"));
                kyLineState.setWorkShopNo(rs.getString("workShopNo"));
                list.add(kyLineState);
            }
//            log.info("EMOS产线状态查询数据", JSON.toJSONString(list));
            list.forEach(e -> kyLineStateService.lineStateMonitor(e));
        } catch (Exception e) {
            log.error("EMOS数据库连接失败 {}", e.getMessage());
        } finally {
            JDBCUtils.close(rs, statement, conn);
        }
        return ResultGenerator.genSuccessResult("调用成功");
    }

    public void insert(String vin) {
        if ("dev".equals(env)) {
            log.info("测试环境EMOS队列下发停止");
            return;
        }
        log.info("EMOS对列发送{}",vin);
        TmSysParameter emosDbConfig = tmSysParameterService.getEmosDbConfig();
        EmosCarInfo emosCarInfo = tmOfmOrderService.getEmosCarInfo(vin);
        Connection conn = JDBCUtils.getConnection(emosDbConfig.getParameterValue().trim(), emosDbConfig.getParameterCategory().trim(), emosDbConfig.getNotes1().trim());
        String sql = "insert into tblCarInfo(carVin,carType,carColor,carSkylight,carRudder,carHighBrightFilm,carEv,status) values ("
                + "'" + emosCarInfo.getCarVin() + "','" + emosCarInfo.getCarType() + "','" + emosCarInfo.getCarColor() + "','" + emosCarInfo.getCarSkylight() + "','" + emosCarInfo.getCarRudder() + "','" +
                emosCarInfo.getCarHighBrightFilm() + "','" + emosCarInfo.getCarEv() + "','" + '0' + "')";
        Statement statement = null;
        try {
            statement = conn.createStatement();
            int i = statement.executeUpdate(sql);
            log.info("插入成功{}条",i);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, statement, conn);
        }
    }
}
